package com.livery.app.view.frame;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;

import org.hibernate.Query;
import org.hibernate.Session;

import com.livery.app.controller.AbstractWorkspaceController;
import com.livery.app.main.HibernateUtil;
import com.livery.app.model.AbstractWorkspaceModel;
import com.livery.app.model.observators.BookingObserver;
import com.livery.app.model.observators.CarObserver;
import com.livery.app.model.observators.ClientObserver;
import com.livery.app.model.tables.BookingModel;
import com.livery.app.model.tables.CarTableModel;
import com.livery.app.model.tables.ClientModel;
import com.livery.data.Booking;
import com.livery.data.Car;
import com.livery.data.Client;

public class BookingFrame extends JFrame implements BookingObserver, 
CarObserver, ClientObserver, ActionListener, ListSelectionListener{

	private JButton btnAddBooking;
	private JButton btnArchiveBooking;
	private JButton btnEditBooking;
	
	private JButton btnSearchBooking;
	private JButton btnSearchClient;
	private JButton btnSearchCar;
	
	private JTextField fieldClient;
	private JTextField fieldCar;
	private JTextField fieldDate;
	
	private JTextField fieldMark;
	private JTextField fieldModel;
	private JTextField fieldYear;
	
	private JTextField fieldName;
	private JTextField fieldSurname;
	private JTextField fieldID;
	
	private JTable tableBooking;
	private JTable tableClient;
	private JTable tableCar;
	
	private ListSelectionModel selectionModelClient, selectionModelCar,
	selectionModelBooking;
	private boolean selectClient = false;
	private boolean selectCar = false;
	private List<Booking> listBooking = new ArrayList<Booking>();
	private List<Client> listClient = new ArrayList<Client>();
	private List<Car> listCar = new ArrayList<Car>();
	
	private TableModel modelBooking = new BookingModel(listBooking);
	private TableModel modelClient = new ClientModel(listClient);
	private TableModel modelCar = new CarTableModel(listCar);
	
	
	private AbstractWorkspaceController controller;
	public BookingFrame(AbstractWorkspaceController controller){
		super("Rezerwacje");
		this.setMinimumSize(new Dimension(800,600));
		this.addWindowListener	(new WindowAdapter(){ 
			public void windowClosing(WindowEvent e){ 
				setVisible(false);				
			}	
		});	

		Dimension dialogSize = getSize();		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
		if(dialogSize.height > screenSize.height) 
			dialogSize.height = screenSize.height;
		if(dialogSize.width > screenSize.width)
			dialogSize.height = screenSize.width;
		setLocation((screenSize.width-dialogSize.width)/2,   
						(screenSize.height-dialogSize.height)/2);
		this.controller = controller;
		createView();

	}
	
	private void createView(){
		this.setLayout(new BorderLayout());
		JTabbedPane tabPane = new JTabbedPane();
		tabPane.add("Dodaj", createTabAdd());
		tabPane.add("Edytuj / Archiwizuj", createTabEditArchive());
		this.add(tabPane);
		
	}
	
	private JButton createButton(String text, String toolTip){
		JButton button = new JButton(text);
		button.setToolTipText(toolTip);
		button.addActionListener(this);
		button.setEnabled(true);
		return button;
	}
	private JPanel createNorthPanelEditArchive(){
		JPanel panel = new JPanel(new FlowLayout());
		JLabel labelClient = new JLabel("Klient:", SwingConstants.RIGHT);
		JLabel labelCar = new JLabel("Samochód:", SwingConstants.RIGHT);
		JLabel labelDate = new JLabel("Data:", SwingConstants.RIGHT);
		fieldClient = new JTextField(10);
		fieldCar = new JTextField(10);
		fieldDate = new JTextField(10);
		btnSearchBooking = createButton("Szukaj", "Wyszukaj rezerwacji");
		panel.add(labelClient);
		panel.add(fieldClient);
		panel.add(labelCar);
		panel.add(fieldCar);
		panel.add(labelDate);
		panel.add(fieldDate);
		panel.add(btnSearchBooking);
		return panel;
	}
	public void setDisabledButtons(){
		setClearSelection();
		btnAddBooking.setEnabled(false);
		btnArchiveBooking.setEnabled(false);
		btnEditBooking.setEnabled(false);
	}
	private void setClearSelection(){
		tableClient.clearSelection();
		tableCar.clearSelection();
		tableBooking.clearSelection();
		selectCar = false;
		selectClient = false;
	}
	private JPanel createSouthPanelEditArchive(){
		JPanel panel = new JPanel(new FlowLayout());
		btnEditBooking = createButton("Edytuj","Edytuj aktualnie zaznaczoną rezerwacje" );
		btnArchiveBooking = createButton("Archiwizuj", "Przenieś aktualnie zaznaczoną rezerwacje do archiwum");
		panel.add(btnEditBooking);
		panel.add(btnArchiveBooking);
		return panel;
	}
	private JPanel createTabEditArchive(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(createNorthPanelEditArchive(), BorderLayout.NORTH);
		panel.add(createCenterPanelEditArchive(), BorderLayout.CENTER);
		panel.add(createSouthPanelEditArchive(), BorderLayout.SOUTH);
		return panel;
	}
	private JPanel createCenterPanelEditArchive(){
		JPanel panel = new JPanel(new BorderLayout());
		tableBooking = new JTable(modelBooking);
		tableBooking.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		selectionModelBooking = tableBooking.getSelectionModel();
		selectionModelBooking.addListSelectionListener(this);
		JScrollPane scrollPane = new JScrollPane(tableBooking);
		panel.add(scrollPane);
		return panel;
	}
	private JPanel createNorthPanelAdd(){
		JPanel panel = new JPanel(new GridLayout(2,1,10,0));
		JPanel panelSearchClient = new JPanel(new FlowLayout());
		JPanel panelSearchCar = new JPanel(new FlowLayout());
		
		JLabel labelName = new JLabel("Imię:", SwingConstants.RIGHT);
		JLabel labelSurname = new JLabel("Nazwisko:", SwingConstants.RIGHT);
		JLabel labelID = new JLabel("Nr dowodu:", SwingConstants.RIGHT);
		fieldName = new JTextField(10);
		fieldSurname = new JTextField(10);
		fieldID = new JTextField(10);
		btnSearchClient = createButton("Szukaj", "Wyszukaj klientów");
		panelSearchClient.add(labelName);
		panelSearchClient.add(fieldName);
		panelSearchClient.add(labelSurname);
		panelSearchClient.add(fieldSurname);
		panelSearchClient.add(labelID);
		panelSearchClient.add(fieldID);
		panelSearchClient.add(btnSearchClient);
		
		JLabel labelMark = new JLabel("Marka:", SwingConstants.RIGHT);
		JLabel labelModel = new JLabel("Model:", SwingConstants.RIGHT);
		JLabel labelYear = new JLabel("Rok produkcji:", SwingConstants.RIGHT);
		fieldMark = new JTextField(10);
		fieldModel = new JTextField(10);
		fieldYear = new JTextField(10);
		btnSearchCar = createButton("Szukaj", "Wyszukaj samochody");
		panelSearchCar.add(labelMark);
		panelSearchCar.add(fieldMark);
		panelSearchCar.add(labelModel);
		panelSearchCar.add(fieldModel);
		panelSearchCar.add(labelYear);
		panelSearchCar.add(fieldYear);
		panelSearchCar.add(btnSearchCar);
		panel.add(panelSearchClient);
		panel.add(panelSearchCar);
		return panel;
	}
	private JPanel createSouthPanelAdd(){
		JPanel panel = new JPanel(new FlowLayout());
		btnAddBooking = createButton("Dodaj", "Dodaj nową rezerwacje");
		panel.add(btnAddBooking);
		return panel;
	}
	private JPanel createCenterPanelAdd(){
		JPanel panel = new JPanel(new BorderLayout());
		JPanel panelTable = new JPanel(new GridLayout(2,1,20,10));
		controller.downloadClientList(0, 0);
		controller.downloadCarList(0, 0);
		modelCar = new CarTableModel(listCar);
		tableClient = new JTable(modelClient);
		tableClient.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableCar = new JTable(modelCar);
		tableCar.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		selectionModelClient = tableClient.getSelectionModel();
		selectionModelCar = tableCar.getSelectionModel();
		
		selectionModelClient.addListSelectionListener(this);
		selectionModelCar.addListSelectionListener(this);
		JScrollPane scrollPaneClient = new JScrollPane(tableClient);
		JScrollPane scrollPaneCar = new JScrollPane(tableCar);
		scrollPaneClient.setBorder(BorderFactory.createTitledBorder("Lista klientów"));
		scrollPaneCar.setBorder(BorderFactory.createTitledBorder("Lista samochodów"));
		panelTable.add(scrollPaneClient);
		panelTable.add(scrollPaneCar);
		panel.add(panelTable, BorderLayout.CENTER);
		return panel;
	}
	private JPanel createTabAdd(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(createNorthPanelAdd(), BorderLayout.NORTH);
		panel.add(createCenterPanelAdd(), BorderLayout.CENTER);
		panel.add(createSouthPanelAdd(), BorderLayout.SOUTH);
		return panel;
	}
	private void setEnabledButtonAdd(){
		if(selectCar && selectClient){
			btnAddBooking.setEnabled(true);
		}
	}
	private void setEnabledButtonEditArchive(){
		btnEditBooking.setEnabled(true);
		btnArchiveBooking.setEnabled(true);
	}
	@Override
	public void updateBooking(List<Booking> list) {
		listBooking = list;
		((BookingModel)modelBooking).setListBooking(listBooking);
		tableBooking.revalidate();
		
	}
	@Override
	public void updateClient(List<Client> list) {
		listClient = list;
		((ClientModel)modelClient).setListClient(listClient);
		tableClient.revalidate();
		
	}
	@Override
	public void updateCar(List<Car> list) {
		listCar = list;
		((CarTableModel)modelCar).setCarList(listCar);
		tableCar.revalidate();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSearchClient){
			String name = fieldName.getText();
			String surname = fieldSurname.getText();
			String documentID = fieldID.getText();
			controller.searchClient(name, surname, documentID);
		}
		if(e.getSource() == btnSearchCar){
			Date date = null;
			String make = fieldMark.getText();
			String model = fieldModel.getText();
			String year = fieldYear.getText();
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				date = format.parse(year);
			} catch (ParseException e1) {

			}
			controller.searchCar(make, model, date);
		}
		
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if(e.getSource() == selectionModelCar){
			selectCar = true;
			setEnabledButtonAdd();	
		}
		if(e.getSource() == selectionModelClient){
			selectClient = true;
			setEnabledButtonAdd();
		}
		if(e.getSource() == selectionModelBooking){
			setEnabledButtonEditArchive();
		}
	}

}
